/**
 * Created by WYH on 17/4/24.
 */
angular.module('tribalc').controller('RentManagerController', [
    '$rootScope' ,'$scope', 'Api', 'Alert', 'RentProtocolModel',
    function ($rootScope ,$scope, Api, Alert, RentProtocolModel) {


        setupRentList();


        $scope.hideRentButton = true;
        $scope.hideSearch = true;
        $scope.nextSkip = null;
        $scope.hasMore = false;
        $scope.pageNum = 0;
        $scope.clickAdd = clickAdd;
        $scope.nextPageClick = nextPageClick;

        function nextPageClick() {
            if ($scope.hasMore == true)
            {
                getRentList($scope.nextSkip);
            }
            else
            {
                Alert.alertBox("已经是最后一页",null);
            }
        }

        function clickAdd() {
            $scope.showRentAdd = true;
        }

        function setupRentList() {
            $rootScope.listenerRootCompanyInfo =  $rootScope.$watch('RootCompanyInfo', function (newCompanyInfo) {
                if (newCompanyInfo && newCompanyInfo.hasOwnProperty('id') && newCompanyInfo.id && newCompanyInfo.propertyNature==true) {
                    getRentList(null);
                }
            })
        }


        function getRentList(sortSkip) {
            Api.getRentList($rootScope.RootCompanyInfo.communityId, null, null, sortSkip, null, function (result) {
                console.log('------list')
                console.log(result)
                $scope.pageNum ++;
                $scope.rentProtocolList = RentProtocolModel.setupRentProtocolList(result.data.content);
                $scope.nextSkip = result.data.nextSkip;
                $scope.hasMore = result.data.hasMore;
            })
        }
        

    }]);